home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / bf / bf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-16  |  15.5 KB  |  582 lines

  1. /* 
  2.  * bf.c --
  3.  *
  4.  *    This program is a test of the "bf" macros.  Nothing is printed if
  5.  *    all errors are successful, otherwise an error message is printed.
  6.  *
  7.  * Copyright 1990 Regents of the University of California
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /sprite/lib/forms/RCS/proto.c,v 1.3 90/01/12 12:03:36 douglis Exp $ SPRITE (Berkeley)";
  19. #endif /* not lint */
  20.  
  21. #include "kernel/bf.h"
  22.  
  23.  
  24. /*
  25.  *----------------------------------------------------------------------
  26.  *
  27.  * FirstBitInFour --
  28.  *
  29.  *    Returns the offset, from the left, of the first bit that is
  30.  *    set in the 4 least significant bits of an integer.
  31.  *
  32.  * Results:
  33.  *    Left offset of first set bit, -1 if there isn't one.
  34.  *
  35.  * Side effects:
  36.  *    None.
  37.  *
  38.  *----------------------------------------------------------------------
  39.  */
  40.  
  41. int
  42. FirstBitInFour(value)
  43.     unsigned int value;        /* The value in which to find the bit. */
  44. {
  45.     static int    first[16] = {-1, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
  46.  
  47.     return first[(value & 0xf)];
  48. }
  49.  
  50. /*
  51.  *----------------------------------------------------------------------
  52.  *
  53.  * FirstBitSet --
  54.  *
  55.  *    Returns the left offset of the first bit that is set in the 
  56.  *    given array of bytes.
  57.  *
  58.  * Results:
  59.  *    Left offsett of first set bit, -1 if there isn't one.
  60.  *
  61.  * Side effects:
  62.  *    None.
  63.  *
  64.  *----------------------------------------------------------------------
  65.  */
  66.  
  67. int
  68. FirstBitSet(size, bytePtr)
  69.     int            size;        /* Number of bytes in the array. */
  70.     unsigned char    *bytePtr;     /* The array of bytes. */
  71. {
  72.     int        offset;
  73.     int    i;
  74.  
  75.     for (i = 0; i < size; i++) {
  76.     offset = FirstBitInFour(((bytePtr[i]) >> 4) & 0xf);
  77.     if (offset != -1) {
  78.         return (offset + (i * 8));
  79.     }
  80.     offset = FirstBitInFour((bytePtr[i]) & 0xf);
  81.     if (offset != -1) {
  82.         return (offset + 4 + (i * 8));
  83.     }
  84.     }
  85.     return -1;
  86. }
  87.  
  88. /*
  89.  *----------------------------------------------------------------------
  90.  *
  91.  * FirstUnsetBitInFour --
  92.  *
  93.  *    Returns the offset, from the left, of the first bit that is
  94.  *    unset in the 4 least significant bits of an integer.
  95.  *
  96.  * Results:
  97.  *    Left offset of first unset bit, -1 if there isn't one.
  98.  *
  99.  * Side effects:
  100.  *    None.
  101.  *
  102.  *----------------------------------------------------------------------
  103.  */
  104.  
  105. int
  106. FirstUnsetBitInFour(value)
  107.     unsigned int value;        /* The value in which to find the bit. */
  108. {
  109.     static int    first[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, -1};
  110.  
  111.     return first[(value & 0xf)];
  112. }
  113.  
  114. /*
  115.  *----------------------------------------------------------------------
  116.  *
  117.  * FirstBitUnset --
  118.  *
  119.  *    Returns the left offset of the first bit that is unset in the 
  120.  *    given array of bytes.
  121.  *
  122.  * Results:
  123.  *    Left offsett of first unset bit, -1 if there isn't one.
  124.  *
  125.  * Side effects:
  126.  *    None.
  127.  *
  128.  *----------------------------------------------------------------------
  129.  */
  130.  
  131. int
  132. FirstBitUnset(size, bytePtr)
  133.     int            size;        /* Number of bytes in the array. */
  134.     unsigned char    *bytePtr;     /* The array of bytes. */
  135. {
  136.     int        offset;
  137.     int    i;
  138.  
  139.     for (i = 0; i < size; i++) {
  140.     offset = FirstUnsetBitInFour(((bytePtr[i]) >> 4) & 0xf);
  141.     if (offset != -1) {
  142.         return (offset + (i * 8));
  143.     }
  144.     offset = FirstUnsetBitInFour((bytePtr[i]) & 0xf);
  145.     if (offset != -1) {
  146.         return (offset + 4 + (i * 8));
  147.     }
  148.     }
  149.     return -1;
  150. }
  151.  
  152. /*
  153.  *----------------------------------------------------------------------
  154.  *
  155.  * NumBitsSetInFour --
  156.  *
  157.  *    Returns the number of bits set in the 4 least significant bits
  158.  *    of the value.
  159.  *
  160.  * Results:
  161.  *    The number of bits that are set.
  162.  *
  163.  * Side effects:
  164.  *    None.
  165.  *
  166.  *----------------------------------------------------------------------
  167.  */
  168.  
  169. int
  170. NumBitsSetInFour(value)
  171.     unsigned int    value;     /* The value in which to count set bits. */
  172. {
  173.     static int set[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
  174.  
  175.     return (set[(value) & 0xf]);
  176. }
  177.  
  178.  
  179. /*
  180.  *----------------------------------------------------------------------
  181.  *
  182.  * NumBitsSet --
  183.  *
  184.  *    Returns the number of bits set in the given array of bytes.
  185.  *
  186.  * Results:
  187.  *    Number of bits set in the given string of bytes.
  188.  *
  189.  * Side effects:
  190.  *    None.
  191.  *
  192.  *----------------------------------------------------------------------
  193.  */
  194.  
  195. int
  196. NumBitsSet(size, bytePtr)
  197.     int            size;        /* Number of bytes in the array. */
  198.     unsigned char    *bytePtr;     /* The array of bytes. */
  199. {
  200.     int        i;
  201.     int        count = 0;
  202.  
  203.     for (i = 0; i < size; i++) {
  204.     count += NumBitsSetInFour(bytePtr[i]);
  205.     count += NumBitsSetInFour(bytePtr[i] >> 4);
  206.     }
  207.     return count;
  208. }
  209.  
  210. /*
  211.  *----------------------------------------------------------------------
  212.  *
  213.  * NumBitsSetContig --
  214.  *
  215.  *    Returns the number of contiguous set bits starting at the given
  216.  *    left offset in the given array of bytes.
  217.  *
  218.  * Results:
  219.  *    The number of contiguous set bits.
  220.  *
  221.  * Side effects:
  222.  *    None.
  223.  *
  224.  *----------------------------------------------------------------------
  225.  */
  226.  
  227. int
  228. NumBitsSetContig(offset, size, bytePtr)
  229.     int            offset;        /* Left offset of start. */
  230.     int            size;        /* Number of bytes in the array. */
  231.     unsigned char    *bytePtr;    /* The array of bytes. */
  232. {
  233.     int     index;
  234.     int        i;
  235.     int        j;
  236.     int        curOffset;
  237.     int        count = 0; 
  238.  
  239.     index = offset / 8;
  240.     curOffset = offset & 0x7;
  241.  
  242.     for( i = index; i < size; i++) {
  243.     for(j = 0; j < 8 - curOffset; j++) {
  244.         if ((bytePtr[i] << (curOffset + j) & 0x80) == 0) {
  245.         return count;
  246.         }
  247.         count++;
  248.     }
  249.     curOffset = 0;
  250.     }
  251.     return count;
  252. }
  253.  
  254.  
  255. /*
  256.  *----------------------------------------------------------------------
  257.  *
  258.  * NumBitsUnsetContig --
  259.  *
  260.  *    Returns the number of contiguous unset bits starting at the given
  261.  *    left offset in the given array of bytes.
  262.  *
  263.  * Results:
  264.  *    The number of contiguous unset bits.
  265.  *
  266.  * Side effects:
  267.  *    None.
  268.  *
  269.  *----------------------------------------------------------------------
  270.  */
  271.  
  272. int
  273. NumBitsUnsetContig(offset, size, bytePtr)
  274.     int            offset;        /* Left offset of start. */
  275.     int            size;        /* Number of bytes in the array. */
  276.     unsigned char    *bytePtr;    /* The array of bytes. */
  277. {
  278.     int     index;
  279.     int        i;
  280.     int        j;
  281.     int        curOffset;
  282.     int        count = 0; 
  283.  
  284.     index = offset / 8;
  285.     curOffset = offset & 0x7;
  286.  
  287.     for( i = index; i < size; i++) {
  288.     for(j = 0; j < 8 - curOffset; j++) {
  289.         if ((bytePtr[i] << (curOffset + j) & 0x80) != 0) {
  290.         return count;
  291.         }
  292.         count++;
  293.     }
  294.     curOffset = 0;
  295.     }
  296.     return count;
  297. }
  298.  
  299.  
  300.  
  301. /*
  302.  *----------------------------------------------------------------------
  303.  *
  304.  * main --
  305.  *
  306.  *    Tries  Set,  Test, and  Get on all possible bit fields
  307.  *    of size 1 through 16 in a piece of memory of a given size.
  308.  *
  309.  * Results:
  310.  *    None.
  311.  *
  312.  * Side effects:
  313.  *    None.
  314.  *
  315.  *----------------------------------------------------------------------
  316.  */
  317.  
  318. int
  319. main(argc, argv)
  320.     int        argc;
  321.     char    **argv;
  322. {
  323.  
  324.     unsigned char    bits[8];
  325.     unsigned short    *shortPtr = (unsigned short *) bits;
  326.     unsigned int    *wordPtr = (unsigned int *) bits;
  327.  
  328.     int     offset;
  329.     int        size;
  330.     int        bitsSet;
  331.     int        loffset;
  332.     int        contig;
  333.     int        test;
  334.     int        value;
  335.     int     i;
  336.  
  337.     /*
  338.      * Test setting of bits, all possible offsets and sizes, in an array
  339.      * of bytes. 
  340.      */
  341.     bzero(bits, sizeof(bits));
  342.     for (size = 1; size < 16; size ++) {
  343.     for (offset = 0; (offset + size) <= sizeof(bits) * 8; offset++) {
  344.         ByteSet(bits, offset, size, (1 << size) - 1);
  345.         bitsSet = NumBitsSet(sizeof(bits), bits);
  346.         if (bitsSet != size) {
  347.         printf("B<%d, %d>, Incorrect number of bits set: %d != %d\n", 
  348.             offset, size, bitsSet, size);
  349.         }
  350.         loffset = FirstBitSet(sizeof(bits), bits);
  351.         if (loffset != offset) {
  352.         printf("B<%d, %d>, Incorrect offset: %d != %d\n", 
  353.             offset, size, loffset, offset);
  354.         }
  355.         contig = NumBitsSetContig(loffset, sizeof(bits), bits);
  356.         if (contig != size) {
  357.         printf("B<%d, %d>, Incorrect contig bits set: %d != %d\n", 
  358.             offset, size, contig, size);
  359.         }
  360.         test = ByteTest(bits, offset, size, (1 << size) - 1);
  361.         if (test != 1) {
  362.         printf("B<%d, %d>,  Test failed\n", offset, size);
  363.         }
  364.         value = ByteGet(bits, offset, size);
  365.         if (value != (1 << size) - 1) {
  366.         printf("B<%d, %d>,  Get failed: %d != %d\n", 
  367.             offset, size, value, (1 << size) - 1);
  368.         }
  369.         ByteSet(bits, offset, size, 0);
  370.         bitsSet = NumBitsSet(sizeof(bits), bits);
  371.         if (bitsSet != 0) {
  372.         printf("B<%d, %d>, %d bits still set\n", offset, size, bitsSet);
  373.         }
  374.     }
  375.     }
  376.     /*
  377.      * Test unsetting of bits, all possible offsets and sizes, in an array
  378.      * of bytes. 
  379.      */
  380.     for (i = 0; i < sizeof(bits); i++) {
  381.     bits[i] = 0xff;
  382.     }
  383.     for (size = 1; size < 16; size ++) {
  384.     for (offset = 0; (offset + size) <= sizeof(bits) * 8; offset++) {
  385.          ByteSet(bits, offset, size, 0);
  386.         bitsSet = NumBitsSet(sizeof(bits), bits);
  387.         if (bitsSet != (sizeof(bits) * 8) - size) {
  388.         printf("B<%d, %d>, Incorrect number of bits unset: %d != %d\n", 
  389.             offset, size, bitsSet, size);
  390.         }
  391.         loffset = FirstBitUnset(sizeof(bits), bits);
  392.         if (loffset != offset) {
  393.         printf("B<%d, %d>, Incorrect offset: %d != %d\n", 
  394.             offset, size, loffset, offset);
  395.         }
  396.         contig = NumBitsUnsetContig(loffset, sizeof(bits), bits);
  397.         if (contig != size) {
  398.         printf("B<%d, %d>, Incorrect contig bits unset: %d != %d\n", 
  399.             offset, size, contig, size);
  400.         }
  401.         test =  ByteTest(bits, offset, size, 0);
  402.         if (test != 1) {
  403.         printf("B<%d, %d>,  Test failed\n", offset, size);
  404.         }
  405.         value =  ByteGet(bits, offset, size);
  406.         if (value != 0) {
  407.         printf("B<%d, %d>,  Get failed: %d != %d\n", 
  408.             offset, size, value, 0);
  409.         }
  410.          ByteSet(bits, offset, size, (1 << size) - 1);
  411.         bitsSet = NumBitsSet(sizeof(bits), bits);
  412.         if (bitsSet != (sizeof(bits) * 8)) {
  413.         printf("B<%d, %d>, %d bits still unset\n", offset, size, 
  414.             (sizeof(bits) * 8) - bitsSet);
  415.         }
  416.     }
  417.     }
  418.     /*
  419.      * Test setting of bits, all possible offsets and sizes, in an array
  420.      * of shorts. 
  421.      */
  422.     bzero(bits, sizeof(bits));
  423.     for (size = 1; size < 16; size ++) {
  424.     for (offset = 0; (offset + size) <= sizeof(bits) * 8; offset++) {
  425.          HalfwordSet(shortPtr, offset, size, (1 << size) - 1);
  426.         bitsSet = NumBitsSet(sizeof(bits), bits);
  427.         if (bitsSet != size) {
  428.         printf("H<%d, %d>, Incorrect number of bits set: %d != %d\n", 
  429.             offset, size, bitsSet, size);
  430.         }
  431.         loffset = FirstBitSet(sizeof(bits), bits);
  432.         if (loffset != offset) {
  433.         printf("H<%d, %d>, Incorrect offset: %d != %d\n", 
  434.             offset, size, loffset, offset);
  435.         }
  436.         contig = NumBitsSetContig(loffset, sizeof(bits), bits);
  437.         if (contig != size) {
  438.         printf("H<%d, %d>, Incorrect contig bits set: %d != %d\n", 
  439.             offset, size, contig, size);
  440.         }
  441.         test =  HalfwordTest(shortPtr, offset, size, (1 << size) - 1);
  442.         if (test != 1) {
  443.         printf("H<%d, %d>,  Test failed\n", offset, size);
  444.         }
  445.         value =  HalfwordGet(shortPtr, offset, size);
  446.         if (value != (1 << size) - 1) {
  447.         printf("H<%d, %d>,  Get failed: %d != %d\n", 
  448.             offset, size, value, (1 << size) - 1);
  449.         }
  450.          HalfwordSet(shortPtr, offset, size, 0);
  451.         bitsSet = NumBitsSet(sizeof(bits), bits);
  452.         if (bitsSet != 0) {
  453.         printf("H<%d, %d>, %d bits still set\n", offset, size, bitsSet);
  454.         }
  455.     }
  456.     }
  457.     /*
  458.      * Test clearing of bits, all possible offsets and sizes, in an array
  459.      * of shorts. 
  460.      */
  461.     for (i = 0; i < sizeof(bits); i++) {
  462.     bits[i] = 0xff;
  463.     }
  464.     for (size = 1; size < 16; size ++) {
  465.     for (offset = 0; (offset + size) <= sizeof(bits) * 8; offset++) {
  466.          HalfwordSet(shortPtr, offset, size, 0);
  467.         bitsSet = NumBitsSet(sizeof(bits), bits);
  468.         if (bitsSet != (sizeof(bits) * 8) - size) {
  469.         printf("H<%d, %d>, Incorrect number of bits unset: %d != %d\n", 
  470.             offset, size, bitsSet, size);
  471.         }
  472.         loffset = FirstBitUnset(sizeof(bits), bits);
  473.         if (loffset != offset) {
  474.         printf("H<%d, %d>, Incorrect offset: %d != %d\n", 
  475.             offset, size, loffset, offset);
  476.         }
  477.         contig = NumBitsUnsetContig(loffset, sizeof(bits), bits);
  478.         if (contig != size) {
  479.         printf("H<%d, %d>, Incorrect contig bits unset: %d != %d\n", 
  480.             offset, size, contig, size);
  481.         }
  482.         test =  HalfwordTest(shortPtr, offset, size, 0);
  483.         if (test != 1) {
  484.         printf("H<%d, %d>,  Test failed\n", offset, size);
  485.         }
  486.         value =  HalfwordGet(shortPtr, offset, size);
  487.         if (value != 0) {
  488.         printf("H<%d, %d>,  Get failed: %d != %d\n", 
  489.             offset, size, value, 0);
  490.         }
  491.          HalfwordSet(shortPtr, offset, size, (1 << size) - 1);
  492.         bitsSet = NumBitsSet(sizeof(bits), bits);
  493.         if (bitsSet != (sizeof(bits) * 8)) {
  494.         printf("H<%d, %d>, %d bits still unset\n", offset, size, 
  495.             (sizeof(bits) * 8) - bitsSet);
  496.         }
  497.     }
  498.     }
  499.     /*
  500.      * Test setting of bits, all possible offsets and sizes, in an array
  501.      * of words. 
  502.      */
  503.     bzero(bits, sizeof(bits));
  504.     for (size = 1; size < 16; size ++) {
  505.     for (offset = 0; (offset + size) <= sizeof(bits) * 8; offset++) {
  506.          WordSet(wordPtr, offset, size, (1 << size) - 1);
  507.         bitsSet = NumBitsSet(sizeof(bits), bits);
  508.         if (bitsSet != size) {
  509.         printf("W<%d, %d>, Incorrect number of bits set: %d != %d\n", 
  510.             offset, size, bitsSet, size);
  511.         }
  512.         loffset = FirstBitSet(sizeof(bits), bits);
  513.         if (loffset != offset) {
  514.         printf("W<%d, %d>, Incorrect offset: %d != %d\n", 
  515.             offset, size, loffset, offset);
  516.         }
  517.         contig = NumBitsSetContig(loffset, sizeof(bits), bits);
  518.         if (contig != size) {
  519.         printf("W<%d, %d>, Incorrect contig bits set: %d != %d\n", 
  520.             offset, size, contig, size);
  521.         }
  522.         test =  WordTest(wordPtr, offset, size, (1 << size) - 1);
  523.         if (test != 1) {
  524.         printf("W<%d, %d>,  Test failed\n", offset, size);
  525.         }
  526.         value =  WordGet(wordPtr, offset, size);
  527.         if (value != (1 << size) - 1) {
  528.         printf("W<%d, %d>,  Get failed: %d != %d\n", 
  529.             offset, size, value, (1 << size) - 1);
  530.         }
  531.          WordSet(wordPtr, offset, size, 0);
  532.         bitsSet = NumBitsSet(sizeof(bits), bits);
  533.         if (bitsSet != 0) {
  534.         printf("W<%d, %d>, %d bits still set\n", offset, size, bitsSet);
  535.         }
  536.     }
  537.     }
  538.     /*
  539.      * Test clearing of bits, all possible offsets and sizes, in an array
  540.      * of words. 
  541.      */
  542.     for (i = 0; i < sizeof(bits); i++) {
  543.     bits[i] = 0xff;
  544.     }
  545.     for (size = 1; size < 16; size ++) {
  546.     for (offset = 0; (offset + size) <= sizeof(bits) * 8; offset++) {
  547.          WordSet(wordPtr, offset, size, 0);
  548.         bitsSet = NumBitsSet(sizeof(bits), bits);
  549.         if (bitsSet != (sizeof(bits) * 8) - size) {
  550.         printf("W<%d, %d>, Incorrect number of bits unset: %d != %d\n", 
  551.             offset, size, bitsSet, size);
  552.         }
  553.         loffset = FirstBitUnset(sizeof(bits), bits);
  554.         if (loffset != offset) {
  555.         printf("W<%d, %d>, Incorrect offset: %d != %d\n", 
  556.             offset, size, loffset, offset);
  557.         }
  558.         contig = NumBitsUnsetContig(loffset, sizeof(bits), bits);
  559.         if (contig != size) {
  560.         printf("W<%d, %d>, Incorrect contig bits unset: %d != %d\n", 
  561.             offset, size, contig, size);
  562.         }
  563.         test =  WordTest(wordPtr, offset, size, 0);
  564.         if (test != 1) {
  565.         printf("W<%d, %d>,  Test failed\n", offset, size);
  566.         }
  567.         value =  WordGet(wordPtr, offset, size);
  568.         if (value != 0) {
  569.         printf("W<%d, %d>,  Get failed: %d != %d\n", 
  570.             offset, size, value, 0);
  571.         }
  572.          WordSet(wordPtr, offset, size, (1 << size) - 1);
  573.         bitsSet = NumBitsSet(sizeof(bits), bits);
  574.         if (bitsSet != (sizeof(bits) * 8)) {
  575.         printf("W<%d, %d>, %d bits still unset\n", offset, size, 
  576.             (sizeof(bits) * 8) - bitsSet);
  577.         }
  578.     }
  579.     }
  580. }
  581.  
  582.